﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using CalculateModelClasses;
using RayTracingModel;

namespace FileOutput
{
    public class PgOutput : P2mFileOutput
    {

        //StringBuilder sb = new StringBuilder();
        public static void OutPath(List<List<CalculateModelClasses.Path>> Paths, string sPath, string projectName)//需要提供一个接受区域所有路径的信息
        {
            //sPath += projectName;
            string Rxname = Paths[0][0].node[Paths[0][0].node.Count - 1].name;
            string groupnum = Paths[0][0].node[Paths[0][0].node.Count - 1].num.ToString();
            if (Paths[0][0].node[Paths[0][0].node.Count - 1].num < 10)
            {
                groupnum = "0" + groupnum;
            }
            FileStream fs = new FileStream(sPath + projectName + ".pg" + ".t001_0" + Paths[0][0].Txnum + ".r0" + groupnum + ".p2m", FileMode.Create);//如果是接收区域的话，多个接收点组成一组接收机组成一个编号
            StreamWriter sw = new StreamWriter(fs);
            List<PathNode> RxNode = new List<PathNode>();
            for (int i = 0; i < Paths.Count; i++)
            {
                RxNode.Add(Paths[i][0].node[Paths[i][0].node.Count - 1]);
            }
            for (int i = 0; i < Paths.Count; i++)
            {
                OutEveryRxPath(RxNode, Rxname);
            }
            sw.Write(sb);
            sb.Clear();
            sw.Close();
            fs.Close();
        }
        public static void OutPath1(List<List<CalculateModelClasses.Path>> Paths, string sPath)
        {
            FileStream fs = new FileStream(sPath + @"PgOutputNull.p2m", FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            sw.Close();
            fs.Close();
        }

        private static void OutEveryRxPath(List<PathNode> RxNode, string Rxname)//接收机的名字,加到path里面,Rxnum接收机的个数
        {
            sb.AppendLine("# Receiver Set:" + Rxname);
            sb.AppendLine("#   Rx#      X(m)           Y(m)          Z(m)     Distance (m) Path Gain (dB)");
            for (int i = 0; i < RxNode.Count; i++)
            {
                sb.AppendLine("     " + (i + 1) + "  " + RxNode[i].position.x.ToString("0.#######E+00") + "  " + RxNode[i].position.y.ToString("0.#######E+00") + "     " + RxNode[i].position.z.ToString("f3") + "       " + Intersection.Distance(RxNode[0].position, RxNode[RxNode.Count - 1].position).ToString("f2") + "      " + "pathgain");//distance需要调用两点距离公式计算出来。pathgain从哪里获取？
            }


        }
    }
}
